package wypozyczalnia.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.jdbc.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import wypozyczalnia.domain.Genre;

@Repository
public class GenreDao extends GenericDao<Genre> {

	@Autowired
	public GenreDao(SessionFactory sf) {
		super(Genre.class, sf);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Genre> getAll() {
		return getHibernateTemplate().find("from Genre");
	}

	public List<Genre> getGenresByIds(List<String> idList) {
		String query = "from Genre genre ";
		boolean first = true;
		for (String i : idList) {
			if (first) {
				query += "where genre.idGenre=" + i + " ";
				first = false;
			}

			query += " or genre.idGenre=" + i + " ";
		}
		return getHibernateTemplate().find(query);
	}
	
	public void deleteGenre(final Integer id){
		this.getSession().doWork(new Work() {

			public void execute(Connection conn) {
				CallableStatement stmt = null;
				try {
					stmt = conn.prepareCall("call DELETE_GENRE(?,?)");
				} catch (SQLException e1) {

					e1.printStackTrace();
				}
				try {
					stmt.setInt(1, id.intValue());

				} catch (SQLException e) {

					e.printStackTrace();
				}
				try {
					stmt.execute();
				} catch (SQLException e) {

					e.printStackTrace();
				}
			}
		});

}
